package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.scity.beens.MovieBannerBeen;
import com.example.scity.beens.WmdcBannerBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WmdcActivity extends AppCompatActivity {
    Banner wmdcbanner;
    LinearLayout wmdcwmztlinear,wmdcyjfklinear,wmdcdjlinear;
    List<String> imglist=new ArrayList<>();
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    handlerbanner();
                    break;
            }
        }
    };

    private void handlerbanner() {
        wmdcbanner.setImages(imglist);
        wmdcbanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context).load(o).into(imageView);
            }
        });
        wmdcbanner.setDelayTime(2000);
        wmdcbanner.start();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wmdc);
        wmdcbanner=findViewById(R.id.wmdc_banner);
        loadwmdcbanner();
        loadwmdcwmztlinear();
        loadwmdcyjfklinear();
    }
    private void loadwmdcyjfklinear() {
        wmdcyjfklinear=findViewById(R.id.wmdcyjfklinear);
        wmdcyjfklinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(WmdcActivity.this,WmdcYjfkActivity.class));
            }
        });
    }
    private void loadwmdcwmztlinear() {
        wmdcwmztlinear=findViewById(R.id.wmdcwmztlinear);
        wmdcwmztlinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(WmdcActivity.this,WmdcWmztActivity.class));
            }
        });
    }
    private void loadwmdcbanner() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.WMDCBannerList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                WmdcBannerBeen json = new Gson().fromJson(response.body().string(), WmdcBannerBeen.class);
                for (int i = 0;i<json.rows.size();i++){
                    imglist.add(Configs.Host+json.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(1);
            }
        });
    }
}